<?php
namespace app\demo_record\controller;

use cmf\controller\HomeBaseController;
use app\demo_record\model\CategoryModel;
use tree\Tree;

class CategoryController extends HomeBaseController
{
    public function fetchTreeSelectOptions($ids="")
    {
        if($ids!==""){
            $ids=explode(",",$ids);
        }
        else{
            $ids=[];
        }
        $model=new CategoryModel();
        $list = $model->db()->select()->toArray();
        foreach($list as &$item){
            $item["selected"] = in_array($item["id"],$ids) ? "selected" : "";
        }

        $tree       = new Tree();
        $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
        $tree->nbsp = '&nbsp;&nbsp;';

        $tree->init($list);
        $option_template = <<<'EOT'
<option value=\"{$id}\" {$selected}>{$spacer}{$title}</option>
EOT;
        $options_content = $tree->getTree(0, $option_template);
        return $options_content;
    }
}
